﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;

using BOSS.Framework.Search.Context;
using BOSS.Framework.Search.UrlBuilder;
using BOSS.Framework.Search.ValueProvider;

namespace BOSS.Framework.Search
{
    /// <summary>
    /// 搜索上下文工厂
    /// </summary>
    class SearchContextFactory : ISearchContextFactory
    {
        /// <summary>
        /// 获取 / 设置 URL 构造器工厂
        /// </summary>
        public IUrlBuilderFactory UrlBuilderFactory { get; set; }

        /// <summary>
        /// 获取 / 设置 缺省值提供器
        /// </summary>
        public IDefaultValueProvider DefaultValueProvider { get; set; }

        /// <summary>
        /// 获取 / 设置 搜索上下文解析器
        /// </summary>
        public ISearchContextParser SearchContextParser { get; set; }

        #region ISearchContextFactory 成员

        /// <summary>
        /// 根据内容类型创建搜索上下文
        /// </summary>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public ISearchContext CreateContext(string contentType)
        {
            return CreateContext(contentType, SearchContext.DEFAULT_ONE_SORT_ONLY);
        }

        /// <summary>
        /// 根据内容类型创建搜索上下文
        /// </summary>
        /// <param name="contentType"></param>
        /// <param name="oneSortOnly"></param>
        /// <returns></returns>
        public ISearchContext CreateContext(string contentType, bool oneSortOnly)
        {
            Check.Require(!string.IsNullOrEmpty(contentType));
            return new SearchContext(contentType, oneSortOnly, this.UrlBuilderFactory, this.DefaultValueProvider);
        }

        /// <summary>
        /// 根据内容类型创建搜索上下文
        /// </summary>
        /// <param name="contentType"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public ISearchContext CreateContext(string contentType, ISearchContext template)
        {
            Check.Require(!string.IsNullOrEmpty(contentType));
            Check.Require(template != null);

            SearchContext context = new SearchContext(contentType, template.OneSortOnly, template.UrlBuilderFactory, template.DefaultValueProvider);
            foreach (IFilterPart filter in template.Filters)
            {
                context.SetFilter(filter);
            }

            context.SetPageSize(template.PageSize);
            return context;
        }

        /// <summary>
        /// 从 URL 中解析得到一个搜索上下文
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public ISearchContext Parse(string url)
        {
            if (this.SearchContextParser == null || this.SearchContextParser == this)
            {
                return null;
            }
            return this.SearchContextParser.Parse(url);
        }

        /// <summary>
        /// 从 URL 中解析得到一个搜索上下文
        /// </summary>
        /// <param name="url"></param>
        /// <param name="oneSortOnly"></param>
        /// <returns></returns>
        public ISearchContext Parse(string url, bool oneSortOnly)
        {
            if (this.SearchContextParser == null || this.SearchContextParser == this)
            {
                return null;
            }
            return this.SearchContextParser.Parse(url, oneSortOnly);
        }

        /// <summary>
        /// 解析参数集合得到一个搜索上下文
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ISearchContext Parse(NameValueCollection parameters)
        {
            if (this.SearchContextParser == null || this.SearchContextParser == this)
            {
                return null;
            }
            return this.SearchContextParser.Parse(parameters);
        }

        /// <summary>
        /// 解析参数集合得到一个搜索上下文
        /// </summary>
        /// <param name="parameters"></param>
        /// <param name="oneSortOnly"></param>
        /// <returns></returns>
        public ISearchContext Parse(NameValueCollection parameters, bool oneSortOnly)
        {
            if (this.SearchContextParser == null || this.SearchContextParser == this)
            {
                return null;
            }
            return this.SearchContextParser.Parse(parameters, oneSortOnly);
        }

        #endregion
    }
}